import {SimpleSchema} from 'meteor/aldeed:simple-schema';
import {Meteor} from 'meteor/meteor';

Meteor.users.deny({
  insert() {
    return true;
  },
  update() {
    return true;
  },
  remove() {
    return true;
  },
});
Meteor.users.schema = new SimpleSchema({
  username: {
    type: String,
    optional: true,
    label: '用户名'
  },
  nickname: {
    type: String,
    optional: true,
    label: '昵称'
  },
  password: {
    type: String,
    optional: true
  },
  roles: {
    type: [String],
    label: '角色',
    optional: true
  },
  relationshipId: {
    type: String,
    label: '亲属关联ＩＤ',
    optional: true
  },
  openId: {
    type: String,
    label: '微信关注生成的openId',
    optional: true
  },
  relationshipType: {
      type: String,
      label: '亲属关联类型',
      optional: true
    },
  familyIds: {
      type: [String],
      label: '所属家庭'
  },
  duration: {
    type: String,
    label: '用户时效',
    optional: true
  },
  userType: { //appUser是app用户， shipUser是app用户自己添加的人（例如新属等）
    type: String,
    label: '用户类型',
  },
  devices: {
    type: [String],
    label: '用户所拥有的锁ＩＤ',
    optional: true
  },
  updatedAt: {
    type: Date,
    optional: true,
    autoValue() {
      if (this.isUpdate) {
        return new Date();
      }
    }
  },
  createdAt: {
    type: Date,
    optional: true,
    label: '创建时间',
    autoValue() {
      if (this.isInsert) {
        return new Date();
      } else if (this.isUpsert) {
        return {$setOnInsert: new Date()};
      }
      return this.unset();  // Prevent user from supplying their own value
    }
  }
});

Meteor.users.attachSchema(Meteor.users.schema);